package com.labot.dao.hibernate;

import java.util.List;








import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.labot.dao.AutoDao;
import com.labot.model.Auto;
import com.labot.model.Centro;

@Repository("AutoDao")
public class AutoDaoHibernate extends HistoryTackerDaoHibernate<Auto, Long> implements AutoDao {

    /**
     * Constructor that sets the entity to User.class.
     */
    public AutoDaoHibernate() {
        super(Auto.class);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public List<Auto> getAutos() {
        Query qry = getSession().createQuery("from Auto c WHERE deletiontime is null order by upper(c.nombre)");
        return qry.list();
    }

    /**
     * {@inheritDoc}
     */
    public Auto saveAuto(Auto auto) {
        if (log.isDebugEnabled()) {
            log.debug("user's id: " + auto.getNombre());
        }
        super.save(auto);
        getSession().flush();
        return auto;
    }

    /**
     * Overridden simply to call the saveUser method. This is happening
     * because saveUser flushes the session and saveObject of BaseDaoHibernate
     * does not.
     *
     * @param user the user to save
     * @return the modified user (with a primary key set if they're new)
     */
    @Override
    public Auto save(Auto auto) {
        return this.saveAuto(auto);
    }


	/**
     * {@inheritDoc}
     * @throws Exception 
    */

	@SuppressWarnings("unchecked")
	@Override
	public Auto buscarAutoPorPatente(String patente) throws Exception {
		List<Auto> autos = getSession().createCriteria(Auto.class).add(Restrictions.eq("patente", patente)).list();
        if (autos == null || autos.isEmpty()) {
            throw new Exception("auto:  '" + patente + "' not found...");
        } else {
            return (Auto)autos.get(0); 
        }
	}

	@Override
	public Auto buscarAuto(Long id) {
		 Query qry = getSession().createQuery("from Auto c WHERE c.id = :id");
		 qry.setParameter("id", id);
		 return (Auto)  qry.list().get(0);
	}

	@Override
	public Auto loadAuto(Long id) {
		// TODO Auto-generated method stub
		return null;
	}
	
    /**
     * {@inheritDoc}
    */
}
